package com.jeffse.light.stream.core.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

/**
 * 线程工具
 */
@Slf4j
public class ScheduleUtils implements Serializable {
    private static ScheduledThreadPoolExecutor threadPoolExecutor = new ScheduledThreadPoolExecutor(10);

    private static Map<String, ScheduledFuture> processTimerMap = new HashMap<>();

    public static ScheduledThreadPoolExecutor getThreadPool(){
        return threadPoolExecutor;
    }

    /**
     * 注册事件
     * @param func
     */
    public static void register(Runnable func, Long delay, String key){
        ScheduledFuture scheduledFuture = ScheduleUtils.getThreadPool().schedule(func, delay, TimeUnit.MILLISECONDS);
        processTimerMap.put(key, scheduledFuture);
    }

    /**
     * 终止线程
     * @param key
     */
    public static void remove(String key){
        log.error("--------------------------  活动的线程： " + threadPoolExecutor.getActiveCount());
        processTimerMap.remove(key);
    }
}
